using Aurora.TaskScheduler.Abstractions.Message;
using Gateway.IApplication.BaseInput;
using Gateway.IApplication.TaskScheduler;
using Gateway.IQuery;
using Gateway.IQuery.TaskScheduler;
using Gateway.IQuery.TaskScheduler.Request;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// TaskScheduler服务
/// </summary>
public class TaskSchedulerController : BaseController
{
    private readonly ITaskSchedulerApp _taskSchedulerApp;
    private readonly ITaskSchedulerQuery _taskSchedulerQuery;

    /// <summary>
    /// 
    /// </summary>
    public TaskSchedulerController(ITaskSchedulerApp taskSchedulerApp, ITaskSchedulerQuery taskSchedulerQuery)
    {
        _taskSchedulerApp = taskSchedulerApp;
        _taskSchedulerQuery = taskSchedulerQuery;
    }

    /// <summary>
    /// 创建task 
    /// </summary>
    /// <param name="input"></param>
    /// <returns>返回主键标识</returns>
    [HttpPost, Route("add")]
    public async Task<string> Add(AppTask input)
    {
        return await _taskSchedulerApp.Add(input);
    }

    /// <summary>
    /// 更新task
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("update")]
    public async Task<bool> Update(AppTask input)
    {
        return await _taskSchedulerApp.Update(input);
    }

    /// <summary>
    /// 删除task
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("remove")]
    public async Task<bool> Remove(RemoveInput input)
    {
        return await _taskSchedulerApp.Remove(input.Id);
    }

    /// <summary>
    /// 运行 task
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("run/{id}")]
    public async Task<bool> Run(string id)
    {
        return await _taskSchedulerApp.Run(id);
    }

    /// <summary>
    /// 暂停 task
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("pause/{id}")]
    public async Task<bool> Pause(string id)
    {
        return await _taskSchedulerApp.Pause(id);
    }

    /// <summary>
    /// 重置 task
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("resume/{id}")]
    public async Task<bool> Resume(string id)
    {
        return await _taskSchedulerApp.Resume(id);
    }

    /// <summary>
    /// 获取所有task 数据列表
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("list")]
    public async Task<List<AppTask>> GetList()
    {
        return await _taskSchedulerQuery.GetList();
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet, Route("detail/{id}")]
    public async Task<AppTask> GetDetail(string id)
    {
        return await _taskSchedulerQuery.GetDetail(id);
    }

    /// <summary>
    /// 获取执行的日志列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost, Route("log/list")]
    public async Task<PageResult<AppTaskLog>> GetLogList(TaskSchedulerLogPageRequest request)
    {
        return await _taskSchedulerQuery.GetLogPageList(request);
    }


    /// <summary>
    /// class job
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("class/job")]
    public async Task<List<string>> GetClassJobDll()
    {
        return await _taskSchedulerQuery.GetClassJobDll();
    }
}